package com.bff.gaia.streaming.api.mo.gaia.io.compondedcoder;

import com.bff.gaia.streaming.api.mo.gaia.io.basiccoder.DoubleCoder;

import java.io.*;

public class MaxMinCoder {
	Double max;
	Double min;
	byte[] data;

	public MaxMinCoder(Double max, Double min) {
		this.max = max;
		this.min = min;
		this.makeBytes();
	}

	public MaxMinCoder(byte[] data) {
		this.data = data;
		this.makeMaxMin();
	}

	private void makeBytes() {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		DataOutputStream dataOutputStream = new DataOutputStream(bos);
		DoubleCoder maxCoder = new DoubleCoder(this.max);
		DoubleCoder minCoder = new DoubleCoder(this.min);
		byte[] maxData = maxCoder.getBytes();
		byte[] minData = minCoder.getBytes();

		try {
			dataOutputStream.writeInt(maxData.length);
			dataOutputStream.write(maxData);
			dataOutputStream.writeInt(minData.length);
			dataOutputStream.write(minData);

		} catch (IOException var8) {
			var8.printStackTrace();
		}

		this.data = bos.toByteArray();
	}

	private void makeMaxMin() {
		DataInputStream din = new DataInputStream(new ByteArrayInputStream(this.data));

		try {
			int maxLen = din.readInt();
			byte[] maxData = new byte[maxLen];
			din.readFully(maxData);
			DoubleCoder maxCoder = new DoubleCoder(maxData);
			this.max = maxCoder.getDouble();
			int minLen = din.readInt();
			byte[] minData = new byte[minLen];
			din.readFully(minData);
			DoubleCoder minCoder = new DoubleCoder(minData);
			this.min = minCoder.getDouble();
		} catch (IOException var8) {
			var8.printStackTrace();
		}

	}

	public Double getMax() {
		return this.max;
	}
	public Double getMin() {
		return this.min;
	}
	public byte[] getBytes() {
		return this.data;
	}
}
